package com.hongmeng.teamwork.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.hongmeng.teamwork.constants.HttpHeadConstant;
import com.hongmeng.teamwork.domain.ITeamworkTaskDomainService;
import com.hongmeng.teamwork.entity.base.Result;
import com.hongmeng.teamwork.entity.dto.TeamworkTaskDto;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Log4j2
@RestController
@RequestMapping("/task")
public class TeamworkTaskController {

    @Resource
    private ITeamworkTaskDomainService teamworkTaskDomainService;

    @PostMapping("/save")
    public Result<?> save(@RequestHeader(HttpHeadConstant.ACCESS_TOKEN) String accessToken,
                            @RequestBody TeamworkTaskDto taskDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeamTaskController.save.dto: {}", JSON.toJSONString(taskDto));
            }
            Preconditions.checkNotNull(taskDto.getProjectId(), "projectId 不能为空！");
            Preconditions.checkArgument(StringUtils.isNotBlank(taskDto.getName()), "任务名不能为空！");
            Preconditions.checkNotNull(taskDto.getCreatedUser(), "任务创建人不能为空！");
            Preconditions.checkNotNull(taskDto.getLeadUser(), "任务负责人不能为空！");
            taskDto.setAccessToken(accessToken);
            teamworkTaskDomainService.save(taskDto);
            return Result.ok("新增任务成功！");
        } catch (Exception e) {
            log.error("TeamTaskController.save.error: {}", e.getMessage(), e);
            return Result.fail("新增任务失败！");
        }
    }

    @PostMapping("/updateTaskStatus")
    public Result<?> updateTaskStatus(@RequestHeader(HttpHeadConstant.ACCESS_TOKEN) String accessToken,
                                        @RequestBody TeamworkTaskDto teamworkTaskDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeamTaskController.updateTaskStatus.dto: {}", JSON.toJSONString(teamworkTaskDto));
            }
            Preconditions.checkNotNull(teamworkTaskDto.getId(), "任务 id 不能为空！");
            Preconditions.checkNotNull(teamworkTaskDto.getStatus(), "任务状态不能为空！");
            teamworkTaskDto.setAccessToken(accessToken);
            teamworkTaskDomainService.updateTaskStatus(teamworkTaskDto);
            return Result.ok("更新任务状态成功！");
        } catch (Exception e) {
            log.error("TeamTaskController.updateTaskStatus.error: {}", e.getMessage(), e);
            return Result.fail("更新任务状态失败！");
        }
    }

    @PostMapping("/direct")
    public Result<?> directTask(@RequestHeader(HttpHeadConstant.ACCESS_TOKEN) String accessToken,
                                @RequestBody TeamworkTaskDto teamworkTaskDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeamTaskController.directTask.dto: {}", JSON.toJSONString(teamworkTaskDto));
            }
            Preconditions.checkNotNull(teamworkTaskDto.getId(), "任务 id 不能为空！");
            Preconditions.checkNotNull(teamworkTaskDto.getLeadUser(), "任务负责人不能为空！");
            teamworkTaskDto.setAccessToken(accessToken);
            teamworkTaskDomainService.directTask(teamworkTaskDto);
            return Result.ok("任务转发成功！");
        } catch (Exception e) {
            log.error("TeamTaskController.updateTaskStatus.error: {}", e.getMessage(), e);
            return Result.fail("任务转发失败！");
        }
    }

    @PostMapping("/query")
    public Result<?> query(@RequestBody TeamworkTaskDto teamworkTaskDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeamTaskController.query.dto: {}", JSON.toJSONString(teamworkTaskDto));
            }
            return Result.ok(teamworkTaskDomainService.query(teamworkTaskDto));
        } catch (Exception e) {
            log.error("TeamTaskController.query.error: {}", e.getMessage(), e);
            return Result.fail("任务转发失败！");
        }
    }

    @GetMapping("/queryInfo")
    public Result<?> queryInfo(Long taskId) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeamworkProjectController.queryInfo.taskId: {}", taskId);
            }
            Preconditions.checkNotNull(taskId, "任务 id 不能为空！");
            return Result.ok(teamworkTaskDomainService.queryInfo(taskId));
        } catch (Exception e) {
            log.error("TeamworkProjectController.queryInfo.error: {}", e.getMessage(), e);
            return Result.fail("查询任务详情失败！");
        }
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody TeamworkTaskDto teamworkTaskDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeamTaskController.query.dto: {}", JSON.toJSONString(teamworkTaskDto));
            }
            teamworkTaskDomainService.update(teamworkTaskDto);
            return Result.ok("任务更新成功！");
        } catch (Exception e) {
            log.error("TeamTaskController.query.error: {}", e.getMessage(), e);
            return Result.fail("任务更新失败！");
        }
    }
}
